import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:希尔排序
 * User: 86183
 * Date: 2023-04-22
 * Time: 17:50
 */
public class sortDeom2 {
    public static void main(String[] args) {
        int[] a = {5,9,489,23,12,56,2,3};
        shellSort(a);
        System.out.println(Arrays.toString(a));
    }
    private static void shellSort(int[] a) {
        int gap = a.length;
        while (gap > 1){
            gap /= 2;
            insertSort(a,gap);
        }
    }

    private static void insertSort(int[] a, int gap) {
        for (int i = gap; i < a.length; i++) {
            int tmp = a[i];
            int j = i - gap;
            for (; j >= 0 ; j-=gap) {
                if(a[j] > tmp){
                    a[j+gap] = a[j];
                }else {
                    break;
                }
            }
            a[j+gap] = tmp;
        }
    }
}
